﻿Public Class AdmDAO
    Dim objAcessoDados As AcessoDados = New AcessoDados(Config.StringConexao)

    Public Function ListaTodos() As List(Of Adm)
        Dim registros As DataSet = New DataSet
        registros = objAcessoDados.ExecutaSqlRetorno("select * from tb_adm")
        Return CriarListaAdm(registros)
    End Function

    Public Function CriarListaAdm(ByVal Registros As DataSet) As List(Of Adm)
        Dim lista As New List(Of Adm)
        Dim objAdm As Adm
        For Each dr As DataRow In Registros.Tables(0).Rows
            objAdm = CriarAdm(dr)
            lista.Add(objAdm)
        Next
        Return lista
    End Function
    Public Function CriarAdm(ByVal row As DataRow) As Adm
        Dim objAdm As New Adm

        objAdm.CodAdm = CInt(row.Item("cod_adm"))
        objAdm.EmailAdm = CStr(row.Item("email_adm"))
        objAdm.NomeAdm = CStr(row.Item("nome_adm"))
        objAdm.SenhaAdm = CStr(row.Item("senha_adm"))

        Return objAdm
    End Function

    Public Function Inserir(ByVal iAdm As Adm) As Integer
        Dim registros As Integer
        Dim sql As New StringBuilder
        sql.Append("insert into tb_adm (")
        sql.Append(" nome_adm, ")
        sql.Append(" email_adm, ")
        sql.Append(" senha_adm ")
        sql.Append(") values (")
        sql.Append("'" & iAdm.NomeAdm.ToString & "',")
        sql.Append("'" & iAdm.EmailAdm.ToString & "',")
        sql.Append("'" & iAdm.SenhaAdm.ToString & "')")
        registros = objAcessoDados.ExecutaSql(sql.ToString)
        Return registros
    End Function

    Public Function LoginAdm(email As String, senha As String) As Adm
        Dim objAdm As Adm
        Dim registro As DataSet = New DataSet

        Dim sql As New StringBuilder
        Dim strWhere As String = " Where "
        sql.Append("select * from tb_adm ")
        sql.Append(strWhere & "email_adm = " & "'" & email.ToString & "'")
        registro = objAcessoDados.ExecutaSqlRetorno(sql.ToString)
        Try
            Dim objAdmData As DataRow = registro.Tables(0).Rows(0)
            objAdm = CriarAdm(objAdmData)
            If objAdm.SenhaAdm = senha Then
                Return objAdm
            End If
        Catch ex As Exception
            Return Nothing
        End Try
        Return Nothing
    End Function

    Public Function Atualizar(ByVal adm As Adm) As Integer
        Dim registros As Integer
        Dim sql As New StringBuilder
        sql.Append("update tb_adm set ")
        sql.Append("nome_adm = '" & adm.NomeAdm.ToString & "', ")
        sql.Append("email_adm = '" & adm.EmailAdm.ToString & "', ")
        sql.Append("senha_adm = '" & adm.SenhaAdm.ToString & "'")
        sql.Append("where cod_adm = " & adm.CodAdm)
        registros = objAcessoDados.ExecutaSql(sql.ToString)
        Return registros
    End Function

End Class
